/**
 * The beta view.
 *
 * (c) Copyright 2011 Nokia Corporation. All rights reserved.
 */

import Qt 4.7

import "../script/common.js" as CommonScript

Rectangle {
    id: betaViewContainer;
    anchors.fill: parent;
    anchors.leftMargin: 10;
    anchors.rightMargin: 10;
    anchors.topMargin: 5;

    // Stores information about what the Back button of THIS VIEW'S CHILDREN
    // does. They can either display the beta list or the search view.
    property int backFunctionality;

    Behavior on opacity {
        PropertyAnimation {
            duration: 500;
        }
    }

    Connections {
        target: btnExitBack;
        // Called when the Exit button is clicked.
        onButtonClicked: {
            // We're only interested in exit functionality here.
            if (btnExitBack.backFunctionality == CommonScript.EXIT) {
                Qt.quit();
            }
        }
    }

    Connections {
        target: tabs;
        onTabClicked: {
            // Beta list can be reloaded only if the last search produced no
            // results.
            if (noResultsInfo.opacity == 1) {
                if (current == CommonScript.TAB_LATEST) {
                    getLatest();
                } else if (current == CommonScript.TAB_POPULAR) {
                    getMostPopular();
                }
            }
        }
    }

    Text {
        id: noResultsInfo;
        width: parent.width;
        text: "No search results found matching your search criteria.";
        opacity: 0;
        wrapMode: "WordWrap";
    }

    ListView {
        id: betaList;
        width: parent.width;
        height: parent.height;
        model: betaModel;
        delegate: betaItem;
        spacing: 0;
        highlight: Rectangle {
            color: "lightsteelblue";
            radius: 5;
            width: betaList.currentItem.width;
            y: betaList.currentItem.y;
        }
        highlightMoveSpeed: 0;
        snapMode: ListView.SnapToItem;
    }

    XmlListModel {
        id: betaModel;
        query: "/betalist/beta";
        onStatusChanged: {
            if (status == XmlListModel.Loading) {
                busyIndicator.opacity = 1;
            } else {
                busyIndicator.opacity = 0;
                // If there are no search results, tell it to the user.
                if (betaList.count == 0) {
                    noResultsInfo.opacity = 1;
                } else {
                    noResultsInfo.opacity = 0;
                }
            }
        }

        XmlRole {
            name: "id";
            query: "id/string()";
        }

        XmlRole {
            name: "title";
            query: "title/string()";
        }

        XmlRole {
            name: "thumb";
            query: "thumb/string()";
        }
    }

    Component {
        id: betaItem;

        Item {
            width: parent.width;
            height: Math.max(betaImage.height + 20, 70);

            Row {
                width: parent.width;
                height: parent.height;
                anchors.top: parent.top;
                anchors.topMargin: 10;
                spacing: 10;

                Image {
                    id: betaImage;
                    source: thumb;
                }
                Text {
                    width: 4 * (parent.width / 5);
                    text: title;
                    wrapMode: "WordWrap";
                }
            }

            Rectangle {
                width: parent.width;
                height: 1;
                border.color: "black";
            }

            MouseArea {
                anchors.fill: parent;
                onClicked: {
                    betaList.currentIndex = index;
                    betaItemClicked(id);
                }
            }
        }
    }

    // Gets the latest betas.
    function getLatest() {
        var url = CommonScript.BETA_LABS_API_URL + "/search"
                + "?state=" + CommonScript.DEFAULT_STATE
                + "&maturity=" + CommonScript.DEFAULT_MATURITY
                + "&order=updated";
        betaModel.source = url;
    }

    // Gets the most popular betas.
    function getMostPopular() {
        var url = CommonScript.BETA_LABS_API_URL + "/search"
                + "?state=" + CommonScript.DEFAULT_STATE
                + "&maturity=" + CommonScript.DEFAULT_MATURITY
                + "&order=rating";
        betaModel.source = url;
    }

    // Gets betas based on the specified tags.
    function getBasedOnTags(tags) {
        var url = CommonScript.BETA_LABS_API_URL + "/search"
                + "?state=" + CommonScript.DEFAULT_STATE
                + "&maturity=" + CommonScript.DEFAULT_MATURITY
                + "&tags=" + tags
                + "&order=title";
        betaModel.source = url;
    }

    // Gets betas based on the specified free text phrase.
    function getBasedOnFreeText(freeTextPhrase) {
        // Encode the phrase string into standard HTML URI format
        freeTextPhrase = escape(freeTextPhrase);

        var url = CommonScript.BETA_LABS_API_URL + "/search-text/"
            + freeTextPhrase;
        betaModel.source = url;
    }

    // Called when an item is selected from the list.
    function betaItemClicked(id) {
        var detailsViewComponent = Qt.createComponent("DetailsView.qml");
        if (detailsViewComponent.status == Component.Ready) {
            busyIndicator.opacity = 1;

            btnExitBack.backFunctionality = this.backFunctionality;

            // Hide the tabs.
            tabs.opacity = 0;

            var detailsViewObject =
                detailsViewComponent.createObject(backgroundImage);

            detailsViewObject.fetchDetails(id);
        }
    }
}
